package com.xiaoyu.tool.configuration.wx;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 配置小程序sdk并且重写sdk的configStorage实现access_token两小时内不过期造成重复刷新问题,可用于分布式
 * */
@Slf4j
@Configuration
@EnableConfigurationProperties(WxMaProperty.class)
public class WxMaConfiguration {

    private final RedisTemplate<String, String> redisTemplate;

    private final WxMaProperty wxMaProperty;

    public WxMaConfiguration(RedisTemplate<String, String> redisTemplate, WxMaProperty wxMaProperty) {
        this.redisTemplate = redisTemplate;
        this.wxMaProperty = wxMaProperty;
    }

    @Bean
    public WxMaService wxMaService(){
        WxMaSpringRedisConfigStorage configStorage = new WxMaSpringRedisConfigStorage(redisTemplate);
        configStorage.setAppId(wxMaProperty.getAppId());
        configStorage.setSecret(wxMaProperty.getAppSecret());
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(configStorage);
        return wxMaService;
    }

}
